<%@page import="dao.Utils"%>
<%@page import="dao.ItemManager"%>
<%@page import="dao.UserManager"%>
<%@page import="model.*"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Virtual Intranet Library</title>
</head>
<body>
	<%@include file='home.jsp'%>
	<%
	    if (request.getParameter("submit") != null)
	    {
			String title = request.getParameter("title");
			String[] types = request.getParameterValues("type");
			String owner = request.getParameter("owner");
			ItemManager manager = new ItemManager();
			List<Item> items = manager.filterItems(title, types, owner);
			if (items == null || items.isEmpty())
			{
			    out.println("<h3>No items match criteria.</h3>");
			} else
			{
	%>
	<table border=1>
		<tr>
			<th>ID</th>
			<th>Title</th>
			<th>Author</th>
			<th>Published</th>
			<th>Owner</th>
			<th>Type</th>
			<th>Available</th>
			<th>Period</th>
		</tr>
		<%
		    for (Item item : items)
				    {
		%>
		<tr>
			<td><%=item.getId()%></td>
			<td><%=item.getTitle()%></td>
			<td><%=item.getAuthor()%></td>
			<td><%=item.getPublishDate()%></td>
			<td><%=item.getOwner()%></td>
			<td><%=item.getType()%></td>
			<td><%=Utils.checkAvailable(item.getState())%></td>
			<td><%=item.getMaxPeriod()%> days</td>
		</tr>
		<%
		    }
		%>
	</table>
	<%
	    }
	    } else
	    {
	%>
	<form method="post">
		Title: <input type="text" name="title"> Type: <select
			name="type" multiple="multiple">
			<%
			    List<ItemType> itemTypes = Arrays.asList(ItemType.values());
					for (ItemType type : itemTypes)
					{
			%>
			<option value="<%=type.getName()%>"><%=type.getName()%></option>
			<%
			    }
			%>
		</select> Owner: <select name="owner">
			<option value="" selected="selected"></option>
			<%
			    List<String> usernames = UserManager.getAllUsernames();
					for (String username : usernames)
					{
			%>
			<option value="<%=username%>"><%=username%></option>
			<%
			    }
			%>
		</select> <input type="submit" name="submit" value="Search">
	</form>
	<%
	    }
	%>

</body>
</html>